package saxion.speelveld.spaceengineer;

import saxion.speelveld.spaceengineer.model.Building;
import saxion.speelveld.spaceengineer.model.BuildingTile;
import saxion.speelveld.spaceengineer.model.ProducingBuilding;
import saxion.speelveld.spaceengineer.model.ResourceWrapper;
import saxion.speelveld.spaceengineer.model.Tile;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomSellPopup extends Activity
{

	private LinearLayout llSellBuildingPopupContainer;

	private ImageView ivSellBuildingImage;

	private TextView tvSellBuildingTitle;

	private TextView tvSellBuildingConfirmationText;

	private Button btnAcceptSellBuilding;

	private Button btnStopSellBuilding;

	private ResourceWrapper resourceArray;

	private Tile selectedTile;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_custom_sell_popup);

		finishActivityWithCancelOnClickListener finishCancelOnClick = new finishActivityWithCancelOnClickListener();

		llSellBuildingPopupContainer = (LinearLayout) findViewById(R.id.llSellBuildingPopupContainer);
		ivSellBuildingImage = (ImageView) findViewById(R.id.ivSellBuildingImage);
		tvSellBuildingTitle = (TextView) findViewById(R.id.tvSellBuildingTitle);
		tvSellBuildingConfirmationText = (TextView) findViewById(R.id.tvSellBuildingConfirmationText);
		btnAcceptSellBuilding = (Button) findViewById(R.id.btnAcceptSellBuilding);
		btnStopSellBuilding = (Button) findViewById(R.id.btnStopSellBuilding);

		llSellBuildingPopupContainer.setOnClickListener(finishCancelOnClick);
		btnStopSellBuilding.setOnClickListener(finishCancelOnClick);
		btnAcceptSellBuilding.setOnClickListener(new finishActivityWithAcceptOnClickListener());

		btnAcceptSellBuilding.setText("Sell");
		btnStopSellBuilding.setText("Cancel");

		Object objResources = getIntent().getSerializableExtra("GameScreen.Resources");
		if (objResources != null)
		{
			resourceArray = (ResourceWrapper) objResources;
		}
		Object objTile = getIntent().getSerializableExtra("GameScreen.Tile");
		if (objTile != null)
		{
			selectedTile = (Tile) objTile;
		}

		if (resourceArray == null || selectedTile == null)
		{
			stopActivityResultCanceled();
		}

		if (selectedTile instanceof BuildingTile)
		{
			BuildingTile buildingTile = ((BuildingTile) selectedTile);
			Building selectedBuilding = buildingTile.getBuilding();
			ivSellBuildingImage.setImageResource(selectedBuilding.getPreviewResourceImageId());
			tvSellBuildingTitle.setText("Sell " + selectedBuilding.getBuildingName());
			if (selectedBuilding instanceof ProducingBuilding)
			{
				tvSellBuildingConfirmationText.setText("Selling this building costs " +
					"will refund 50% of it's resource costs");
			}
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.custom_sell_popup, menu);
		return true;
	}

	private class finishActivityWithAcceptOnClickListener implements OnClickListener
	{

		@Override
		public void onClick(View v)
		{
			Intent returnIntent = new Intent();
			// returnIntent.putExtra("Upgrading.SciencePointCosts", 100);
			setResult(RESULT_OK, returnIntent);
			finish();
		}
	}

	private void stopActivityResultCanceled()
	{
		Intent returnIntent = new Intent();
		setResult(RESULT_CANCELED, returnIntent);
		finish();
	}

	private class finishActivityWithCancelOnClickListener implements OnClickListener
	{

		@Override
		public void onClick(View v)
		{
			stopActivityResultCanceled();
		}
	}

}
